package com.tom.joke.fragment;

import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.tom.baselibrary.base.BaseFragment;
import com.tom.baselibrary.ioc.ViewById;
import com.tom.joke.R;
import com.tom.joke.view.banner.BannerAdapter;
import com.tom.joke.view.banner.BannerView;
import com.tom.joke.view.banner.BannerViewPager;

import java.util.ArrayList;
import java.util.List;

/**
 * Added by Tom on 2024/07/15.
 */
public class HomeFragment extends BaseFragment {

    @ViewById(R.id.banner_view)
    private BannerView mBannerView;

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {
        // 模拟数据，初始化广告位。
        mBannerView.post(new Runnable() {
            @Override
            public void run() {
                initBanner();
            }
        });
    }


    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }


    private void initBanner() {
        List<String> imgList = new ArrayList();
        imgList.add("https://img-home.csdnimg.cn/images/20240218021830.png");
        imgList.add("https://img-home.csdnimg.cn/images/20240627054627.png");
        imgList.add("https://img1.baidu.com/it/u=10309923,1927267730&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1721235600&t=869c24c9e80d6489dad69655a77bd4c0");
        imgList.add("https://img0.baidu.com/it/u=1701402037,3734123211&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1721235600&t=2cc84c2093edb29e9cf779452e3d661a");
        imgList.add("https://img-home.csdnimg.cn/images/20240218021830.png");
        imgList.add("https://img-home.csdnimg.cn/images/20240627054627.png");

        mBannerView.setAdapter(new BannerAdapter() {
            @Override
            public View getView(int position, View convertView) {
                // View复用。
                ImageView bannerIv = null;
                if (convertView == null) { // 复用的view。
                    bannerIv = new ImageView(getActivity());
                    bannerIv.setScaleType(ImageView.ScaleType.FIT_XY);
                } else {
                    // 直接复用
                    bannerIv = (ImageView) convertView;
                    Log.e("TAG", "getView: 界面复用" + bannerIv);
                }
                String imgPath = imgList.get(position);
                Glide.with(getActivity()).load(imgPath)
                        .placeholder(R.drawable.banner_default).into(bannerIv);
                return bannerIv;
            }

            @Override
            public int getCount() {
                return imgList.size();
            }

            @Override
            public String getBannerDesc(int position) {
                return "~~~国琴姐广告~~~ " + position;
            }
        });
        mBannerView.startRoll(); // 开始自动轮播

        mBannerView.setOnBannerItemClickListener(new BannerViewPager.BannerItemClickListener() {
            @Override
            public void click(int position) {
                // 广告图片点击事件回调。
                String item = imgList.get(position);
                Log.d("TAG", "item url:" + item);
                Log.d("TAG", item+" click img item:" + position);
                String msg = "https://img-home.csdnimg.cn/images/20240218021830.png";
            }
        });
    }

}
